package com.aaa.controller;

import com.aaa.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping(value = "/config")
public class Config {

    @GetMapping(value = "")
    public String index(Model model) {
        model.addAttribute("all_data", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        return "config/index.html";
    }

    @PostMapping(value = "/txt_folder")
    @ResponseBody
    public String config(Model model, @RequestBody Map<String, Object> requestData) {
        model.addAttribute("all_data", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

        String txt_folder = String.valueOf(requestData.get("txt_folder"));

        Constants constants = new Constants();
        String setting_file_path = constants.getSETTING_FILE_PATH();

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(setting_file_path))) {
            writer.write(txt_folder);
            return "success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }



}




